package com.weishi.project.training.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weishi.common.utils.DateUtils;
import com.weishi.project.api.ret.AnnualPlanRet;
import com.weishi.project.api.ret.NaturalYearRet;
import com.weishi.project.sport.home.vo.AnnualPlanRetVO;
import com.weishi.project.sport.home.vo.NaturalYearRetVO;
import com.weishi.project.training.domain.AnnualPlan;
import com.weishi.project.training.domain.AnnualProject;
import com.weishi.project.training.domain.NaturalYear;
import com.weishi.project.training.mapper.NaturalYearMapper;
import com.weishi.project.training.service.IAnnualPlanService;
import com.weishi.project.training.service.IAnnualProjectService;
import com.weishi.project.training.service.INaturalYearService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * NaturalYearService业务层处理
 */
@Service
public class NaturalYearServiceImpl extends ServiceImpl<NaturalYearMapper, NaturalYear> implements INaturalYearService {

    @Autowired
    private NaturalYearMapper naturalYearMapper;

    @Autowired
    private IAnnualPlanService annualPlanService;

    /**
     * 年度训练项目
     */
    @Autowired
    private IAnnualProjectService annualProjectService;


    @Override
    public List<NaturalYear> selectNaturalYearList(NaturalYear naturalYear) {
        return naturalYearMapper.selectNaturalYearList(naturalYear);
    }

    @Override
    public NaturalYear getNaturalYearByDate(Date date) {
        List<NaturalYear> list = this.selectNaturalYearList(new NaturalYear());
        for(NaturalYear naturalYear : list){
            if(DateUtils.isDateBetweenOtherDate(naturalYear.getStartTime(),naturalYear.getEndTime(),date)){
                return naturalYear;
            }
        }
        NaturalYear naturalYear = new NaturalYear();
        naturalYear.setId(0);
        naturalYear.setName("未知年度");
        return naturalYear;
    }

    @Override
    public List<NaturalYear> getNaturalYearList() {
        return naturalYearMapper.getNaturalYearList();
    }

    @Override
    public List<NaturalYearRet> selectNaturalYearRetList(boolean weekRetListDate) {
        List<NaturalYearRet> naturalYearRets = new ArrayList<>();
        List<NaturalYear> list = this.selectNaturalYearList(new NaturalYear());
        for(NaturalYear naturalYear : list){
            NaturalYearRet naturalYearRet = new NaturalYearRet(naturalYear);
            List<AnnualPlanRet> annualPlanRets = annualPlanService.selectAnnualPlanRetListByNaturalYearId(naturalYear.getId(),weekRetListDate);
            naturalYearRet.setAnnualPlans(annualPlanRets);
            naturalYearRets.add(naturalYearRet);
        }
        return naturalYearRets;
    }

    @Override
    public NaturalYear getNaturalYearListByNowDate() {
        return naturalYearMapper.getNaturalYearListByNowDate();
    }


    /**
     * 重新封装查询
     */
    @Override
    public List<NaturalYearRetVO> selectNaturalYearRetListVO(boolean weekRetListDate) {
        List<NaturalYearRetVO> naturalYearRets = new ArrayList<>();
        List<NaturalYear> list = this.selectNaturalYearList(new NaturalYear());
        for(NaturalYear naturalYear : list){
            NaturalYearRetVO naturalYearRet = new NaturalYearRetVO(naturalYear);
            List<AnnualPlanRetVO> annualPlanRets = annualPlanService.selectAnnualPlanRetListByNaturalYearIdVO(naturalYear.getId(),weekRetListDate);
            naturalYearRet.setAnnualPlans(annualPlanRets);
            naturalYearRets.add(naturalYearRet);
        }
        return naturalYearRets;
    }



    /**
     * 删除自然年，同时也删除自然年下的周 年度训练项目
     */
    @Transactional
    @Override
    public void removeYearAndWeek(Integer[] ids) {

        if(Arrays.asList(ids).size() > 0)Arrays.asList(ids).forEach( p-> {
            // 删除年度训练项目
            List<AnnualProject> annualProjectList = annualProjectService
                    .list(Wrappers.<AnnualProject>lambdaQuery().eq(AnnualProject::getNaturalYearId , p));
            if(annualProjectList.size() > 0)annualProjectList.forEach( a-> {
                annualProjectService.removeById(a.getId());
            });

            // 删除年下面的周
            List<AnnualPlan> annualPlanList = annualPlanService
                    .list(Wrappers.<AnnualPlan>lambdaQuery()
                            .eq(AnnualPlan::getNaturalYearId , p));
            if(annualPlanList.size() > 0)annualPlanList.forEach( a->{
                    annualPlanService.removeById(a.getId());
            });
        });

        // 删除自然年
        this.removeByIds(Arrays.asList(ids));
    }








}